package com.ymatou.iapi.godmch.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.GetPermissionItemListBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.service.GetPermissionItemListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.Permissionitem;
/**
*获取权限目录列表
*FreeMaker Template自动生成代码
*/
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
@TestSuite
public class Ts_GetPermissionItemList{
	private static GetPermissionItemListBean getpermissionitemlistBean;
	private static GetPermissionItemListCall getpermissionitemlistCall;
	DefGodMchWapper DefGodMchWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取权限目录列表");
	}

	@Before
	public void caseUp() {
		getpermissionitemlistBean=new GetPermissionItemListBean();
		getpermissionitemlistCall=new GetPermissionItemListCall();
		DefGodMchWapper=new DefGodMchWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getpermissionitemlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	//获取权限列表 目前都是默认权限所以MchId输入什么都是一样的 后续个性化会产生不同返回
	public void Tc_GetPermissionItemList_001() {
		Logger.start(true,"获取权限目录列表,成功");
		try {
			getpermissionitemlistBean.setMchId(0);
			getpermissionitemlistCall.setData(getpermissionitemlistBean);
			getpermissionitemlistCall.callService();			
			Logger.verifyEquals("5004", getpermissionitemlistCall.getString("status"), "验证status");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	//@Ignore
	public void Tc_GetPermissionItemList_002() {
		Logger.start(true,"不输入mchId失败");
		try {
			getpermissionitemlistCall.setData(getpermissionitemlistBean);
			getpermissionitemlistCall.callService();			
			Logger.verifyEquals("500", getpermissionitemlistCall.getString("status"), "验证status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPermissionItemList_003() {
		Logger.start(true,"获取权限目录列表,纯代理,返回纯代理权限(纯代理 查询 permissionitem表中mchType=0,2的角色)");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			
			List<createmchBean>  createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			createmchList.get(0).setCountry("CHN");//国内-纯代理
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new createmchBean[createmchList.size()]));
			
			getpermissionitemlistBean.setMchId(userId_agent);
			getpermissionitemlistCall.setData(getpermissionitemlistBean);
			getpermissionitemlistCall.callService();			
			Logger.verifyEquals("200", getpermissionitemlistCall.getString("status"), "验证status");
			return_verify(1);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPermissionItemList_004() {
		Logger.start(true,"获取权限目录列表,渠道,返回渠道权限(渠道 查询 permissionitem表中mchType=0,1的角色)");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchBean);

			getpermissionitemlistBean.setMchId(userId_channl);
			getpermissionitemlistCall.setData(getpermissionitemlistBean);
			getpermissionitemlistCall.callService();			
			Logger.verifyEquals("200", getpermissionitemlistCall.getString("status"), "验证status");
			return_verify(2);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPermissionItemList_005() {
		Logger.start(true,"获取权限目录列表,vip渠道,返回渠道权限(渠道 查询 permissionitem表中mchType=0,1的角色)");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchBean);
            Logger.comment("###### 设置vip渠道 ######");
			GodmchManageCallService.SetVip(userId_channl);
			getpermissionitemlistBean.setMchId(userId_channl);
			getpermissionitemlistCall.setData(getpermissionitemlistBean);
			getpermissionitemlistCall.callService();			
			Logger.verifyEquals("200", getpermissionitemlistCall.getString("status"), "验证status");
			return_verify(2);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPermissionItemList_006() {
		Logger.start(true,"获取权限目录列表,vip渠道+代理,返回渠道权限(渠道 查询 permissionitem表中mchType=0,1的角色)");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			Logger.comment("###### 设置vip渠道 ######");
			GodmchManageCallService.SetVip(userId_channl);
			List<createmchBean>  createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			createmchList.get(0).setCountry("USA");//国内-纯代理
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new createmchBean[createmchList.size()]));
			
			getpermissionitemlistBean.setMchId(userId_agent);
			getpermissionitemlistCall.setData(getpermissionitemlistBean);
			getpermissionitemlistCall.callService();			
			Logger.verifyEquals("200", getpermissionitemlistCall.getString("status"), "验证status");
			
			return_verify(2);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPermissionItemList_007() {
		Logger.start(true,"获取权限目录列表,国内vip代理,返回代理权限(代理 查询 permissionitem表中mchType=0,2的角色)");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			Logger.comment("###### 设置vip渠道 ######");
			GodmchManageCallService.SetVip(userId_channl);
			
			List<createmchBean>  createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			createmchList.get(0).setCountry("CHN");//国内-纯代理
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new createmchBean[createmchList.size()]));
			
			getpermissionitemlistBean.setMchId(userId_agent);
			getpermissionitemlistCall.setData(getpermissionitemlistBean);
			getpermissionitemlistCall.callService();			
			Logger.verifyEquals("200", getpermissionitemlistCall.getString("status"), "验证status");
			return_verify(1);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void return_verify(int not_mchType) throws InterruptedException
	{
		List<Permissionitem> dblist = DefGodMchWapper.selectPermissionitemByPermissionCode(null);
		JsonArray ja = getpermissionitemlistCall.getJsonObject().get("result").getAsJsonObject().get("permissionItemDtos").getAsJsonArray();
		ja.forEach(ele->{
			int permissionCode = ele.getAsJsonObject().get("permissionCode").getAsInt();
			String name = ele.getAsJsonObject().get("name").getAsString();
			int categoryId = ele.getAsJsonObject().get("categoryId").getAsInt();
			String remark = ele.getAsJsonObject().get("remark").getAsString();
			Permissionitem p1 = dblist.stream().filter(p->p.getPermissionCode().equals(permissionCode) && (!p.getMchType().equals(not_mchType))).findFirst().get();
			Logger.verifyEquals(p1.getCategoryId(), categoryId, "验证categoryId");
			Logger.verifyEquals(p1.getPermissionName(), name, "验证name");
			Logger.verifyEquals(p1.getRemark(), remark, "验证remark");
		});
	}
}